﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;
using ZyGames.Framework.Common;
using ZyGames.Framework.Game.Cache;

namespace ZyGames.DZPocker.Model
{
	/// <summary>
	/// 用于下发20020广播玩家剩余筹码
	/// </summary>
	[Serializable, ProtoContract]
	public class PositionChipData
	{
		[ProtoMember(1)]
		public int PosId { get; set; }
		[ProtoMember(2)]
		public int Chip { get; set; }
	}

	/// <summary>
	/// 桌子的座位对象
	/// </summary>
	[Serializable, ProtoContract]
	public class PositionData
	{
		private int _id;
		private int _tableid;
		/// <summary>
		/// 是否机器人或托管玩家
		/// </summary>
		[ProtoMember(1)]
		public bool IsAI { get; set; }

		/// <summary>
		/// 是否是庄家
		/// </summary>
		[ProtoMember(2)]
		public bool IsVillage { get; set; }

		/// <summary>
		/// 是否明牌
		/// </summary>
		[ProtoMember(3)]
		public bool IsShow { get; set; }

		/// <summary>
		///	玩家id
		/// </summary>
		[ProtoMember(4)]
		public int UserId { get; set; }
		/// <summary>
		/// 昵称
		/// </summary>
		[ProtoMember(5)]
		public string NickName { get; set; }
		/// <summary>
		/// 头像
		/// </summary>
		[ProtoMember(6)]
		public string HeadIcon { get; set; }
		/// <summary>
		/// 玩家筹码数量
		/// </summary>
		[ProtoMember(7)]
		public int Chip { get; set; }

		/// <summary>
		/// 是否在玩的状态
		/// </summary>
		public bool State = false;

		/// <summary>
		/// 牌的类型
		/// </summary>
		public DeckType CardsDeckType = DeckType.None;

		/// <summary>
		/// 获得类型之后的牌型大小数组
		/// </summary>
		public int[] CardsValue;

		/// <summary>
		/// 玩的状态
		/// </summary>
		public PositionPlayState PlayState = PositionPlayState.None;

		/// <summary>
		/// 是否是最大下注
		/// </summary>
		public bool MaxBet = false;

		/// <summary>
		/// 是否可以下注
		/// </summary>
		public bool BetState = false;

		private List<int> _cardData;

		/// <summary>
		/// 当前回合下注多少
		/// </summary>
		public int CurrentRoundChip = 0;

		/// <summary>
		/// 当前一手牌总共下注多少
		/// </summary>
		public int CurrentHandBetChip = 0;

		/// <summary>
		/// 临时缓存的玩家当手牌下注的筹码数量
		/// </summary>
		public int CurrentHandTempBetChip = 0;

		/// <summary>
		/// 手牌数据
		/// </summary>
		public List<int> CardData
		{
			get { return _cardData; }
			set { _cardData = value; }
		}

		public PositionData(int id)
		{
			_id = id + 1;
			_cardData = new List<int>();
			this.Init();
		}

		public bool IsEmpty
		{
			get { return UserId == 0; }
		}

		/// <summary>
		/// 初始化
		/// </summary>
		/// <param name="user"></param>
		/// <param name="isAI"></param>
		public void Init(int tableid = 0, GameUser user = null, PositionPlayState playState = PositionPlayState.None, bool isAI = false)
		{
			UserId = user == null ? 0 : user.GetUserId();
			NickName = user == null ? "" : user.GetNickName();
			HeadIcon = user == null ? "" : user.HeadIcon;
			IsAI = isAI;
			this.PlayState = playState;
			if (user != null)
			{
				user.Property.SetTablePositionId(tableid,Id);
				user.Property.AddTableID(tableid);
				this._tableid = tableid;
			}
			//发牌
			ReSendCard();
		}

		/// <summary>
		/// 重新发牌
		/// </summary>
		public void ReSendCard()
		{
			IsShow = false;
			IsVillage = false;
			_cardData.Clear();
		}

		/// <summary>
		/// 座位数据清空
		/// </summary>
		public void PosDataClear()
		{
			this.Chip = 0;
			this.UserId = 0;
			this.CurrentRoundChip = 0;
			this.CurrentHandBetChip = 0;
			this.CurrentHandTempBetChip = 0;
			this.NickName = string.Empty;
			this.IsVillage = false;
			this.IsShow = false;
			this.HeadIcon = string.Empty;

			this.DataReset();
		}

		/// <summary>
		/// 重置本局数据
		/// </summary>
		public void DataReset()
		{
			this.PlayState = PositionPlayState.None;
			this.CardsDeckType = DeckType.None;
			this._cardData.Clear();
			this.CurrentHandBetChip = 0;
			this.CurrentHandTempBetChip = 0;
			if (this.CardsValue != null)
				this.CardsValue.Initialize();
		}

		/// <summary>
		/// 根据玩家的Property的Ready状态来判断该位置是否准备好
		/// </summary>
		/// <returns></returns>
		public bool Ready()
		{
			var user = this.CurrentGameUser();
			if (user == null)
				return false;
			return user.Property.GetReadyState(_tableid);
		}

		/// <summary>
		/// 座位编号
		/// </summary>
		public int Id
		{
			get
			{
				return _id;
			}
		}

		public GameUser CurrentGameUser()
		{
			if (this.UserId == 0)
				return null;
			var gameUser = new GameDataCacheSet<GameUser>().FindKey(UserId.ToString());
			return gameUser;
		}
	}
}
